config = require('../config')
cachedb = config.cachedb
Schema = config.Schema

#交易密码缓存
#_id格式：交易客户编号-会话ID
PasswordCacheSchema = new Schema(
    _id:String
    password_trade:
        password:String
        op_station:String
    tm:
        type: Date
        default: -> new Date()
        index: { expires: 30*60 }
)

PasswordCacheSchema.statics.save = (id, password_trade, cb) ->
    this.model('PasswordCache').update {_id:id}, { password_trade:password_trade, tm:new Date() }, { upsert:true }, cb

PasswordCacheSchema.statics.load = (id,cb) ->
    this.model('PasswordCache').findOne {_id:id}, (err,result) ->
        return cb(err) if err
        console.log "[PasswordCache HIT][#{id}]" if result?.password_trade
        cb(null,result?.password_trade)

PasswordCacheSchema.statics.clear = (id,cb) ->
    #同时清除以这个id开头的所有缓存,例如："交易客户编号-", 可用于清除一个用户在不同会话下的缓存
    this.model('PasswordCache').remove {_id:new RegExp('^'+id) }, cb

#接口数据缓存
#_id格式：交易客户编号-调用参数
ServiceDataCacheSchema = new Schema(
    _id:String,
    data:Schema.Types.Mixed
    tm:
        type: Date
        default: -> new Date()
        index: { expires: 60 }
)

ServiceDataCacheSchema.statics.clear = (id,cb) ->
    #同时清除以这个id开头的所有缓存，例如：可能包括已购、已过期和全部服务产品
    this.model('ServiceDataCache').remove {_id:new RegExp('^'+id) }, cb

ServiceDataCacheSchema.statics.load = (id,cb) ->
    this.model('ServiceDataCache').findOne {_id:id}, (err,result) ->
        return cb(err) if err
        console.log "[ServiceDataCache HIT][#{id}]" if result?.data
        cb(null,result?.data)

ServiceDataCacheSchema.statics.save = (id, data, cb) ->
    this.model('ServiceDataCache').update {_id:id}, { data:data, tm:new Date() }, { upsert:true }, cb

# 返回cache的模型对象  
make_cache = (name,expires) ->
  schema = new Schema
    _id: String
    data: Schema.Types.Mixed
    tm:
      type: Date
      default: -> new Date()
      index:
        expires: expires

  schema.statics.clear = (id,cb) -> 
    @model(name).remove {_id:id},cb

  schema.statics.load = (id,cb) ->
    @model(name).findOne {_id:id},
                         (err,result) ->
                           if err
                             return cb(err)
                           cb(null,result?.data)
  
  schema.statics.save = (id,data,expire,cb) ->
    if typeof expire == "function" and not cb?
      cb = expire
      expire = new Date
    @model(name).update {_id:id}, { data:data, tm:expire }, { upsert:true }, cb
    
  schema.set("autoIndex",false)

  return cachedb.model(name,schema)
  
#PasswordCacheSchema.set('autoIndex', false)
ServiceDataCacheSchema.set('autoIndex', false)

#exports.PasswordCache = cachedb.model('PasswordCache', PasswordCacheSchema)
exports.PasswordCache = make_cache("Password2Cache",0)
exports.ServiceDataCache = cachedb.model('ServiceDataCache', ServiceDataCacheSchema)
exports.QuotesCache = make_cache("QuotesCache", 7*24*3600) # 缓存一周,行情数据不会变化
exports.UserSummaryCache = make_cache("UserSummaryCache",  10*60)
exports.FundMngComCache = make_cache("FundMngComCache",  10*60)
exports.SmsQuotasCache = make_cache("SmsQuotasCache",  24*3600)
exports.PageCache = make_cache("PageCache",0)
exports.LockAccountCache = make_cache("LockAccountCache",0)
exports.RiskLevelCache = make_cache("RiskLevelCache",24*3600*2)
exports.QmemoCache = make_cache("QmemoCache",0)
exports.test_cache = {make_cache}

  
